package com.matt.controller.pad;

import com.matt.domain.Classes;
import com.matt.domain.course.CoursePlanClasses;
import com.matt.foundation.DO.course.CoursePlanClassesDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.commons.dto.sso.StaffDTO;;
import com.matt.commons.exception.BusinessException;

import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.query.ClassesCoursePlanQuery;
import com.matt.foundation.query.FamilyCoursePlanQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.course.CourseAreaBuildVo;
import com.matt.foundation.vo.course.PadCoursePlanDetailVo;
import com.matt.foundation.vo.course.PadTeacherCoursePlanListVo;
import com.matt.service.*;

import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/13
 * *******************************
 */
@RestController
@RequestMapping("/pad/course")
public class PadCourseController {

    @Autowired
    private ClassesService classesService;

    @Autowired
    private CourseEarlyService courseEarlyService;
    @Autowired
    private CourseAreaBulidService courseAreaBuildService;
    @Autowired
    private CourseNurseryService courseNurseryService;
    @Autowired
    private CourseKXThemeService courseKXThemeService;
    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private CoursePlanClassesService coursePlanClassesService ;

    @Autowired
    private CourseService courseService;

    @RequestMapping("/get_plan_list")
    public StatusResponse getPlanList(@RequestHeader(TICKET) String ticket,String classId,String queryType, ClassesCoursePlanQuery query){
        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = null ;
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());
        query.setClassId(classId);
        query.setType(queryType);
//        Boolean isTestUser = ssoCallService.getIsTestUser(baseInfo.getId());

        Classes classes = classesService.selectClassById(classId);
        if(classes == null){
            throw new BusinessException(ErrorCode.CLASSES_NOT_EXIST, "没有找到班级");
        }

        //查询托班今日课程列表
        if(classes.getCategory() == CourseConstant.Category.NURSERY.codeOf()) {
            coursePlanNurseryList = courseNurseryService.getPadTodayCoursePlanList(query, false,queryType,true);
        }
        if(classes.getCategory() == CourseConstant.Category.EARLY.codeOf()) {
            if ("today".equals(queryType)) {
                //查询早教班今日课程列表
                coursePlanNurseryList = courseEarlyService.getPadTodayCoursePlan(query, false);
            } else if ("week".equals(queryType)) {
                //查询早教班本周课程列表
                coursePlanNurseryList = courseEarlyService.getPadWeekCoursePlan(query, false);
            }else if ("nextweek".equals(queryType)) {
                //查询早教班下周课程列表
                coursePlanNurseryList = courseEarlyService.getPadNextWeekCoursePlan(query, false);
            }
        }

        return StatusResponse.success(coursePlanNurseryList);

    }

    //今日、本周、下周 主题课程
    @ApiOperation(value = "家庭端-排课记录")
    @PostMapping("/get_plan_family_list")
    public StatusResponse todayPlanFamilyList(@RequestHeader(TICKET) String ticket,  FamilyCoursePlanQuery query) {
        Page<PadTeacherCoursePlanListVo>  coursePlanNurseryList = courseNurseryService.getPadFamilyTodayCoursePlanList(query,query.getType(),true);
        return StatusResponse.success(coursePlanNurseryList);
    }

    @RequestMapping("/plan_detail")
    public StatusResponse planDetail(String planId) {
        PadCoursePlanDetailVo vo = null ;
        CoursePlanClasses coursePlanClasses = coursePlanClassesService.getOne(planId);
        if (coursePlanClasses == null || Objects.isNull(coursePlanClasses))
            return StatusResponse.error(ErrorCode.TOPIC_NOT_EXIST, "排课信息不存在！");
        
        int courseType = coursePlanClasses.getCourseType();
//        if(coursePlanClasses.getCategory() == CourseConstant.Category.NURSERY.codeOf()&&courseType != CourseConstant.CourseType.AREA_BUILD.codeOf()&&courseType != CourseConstant.CourseType.KX_THEME.codeOf()){
//            vo = courseNurseryService.getPadCoursePlanDetail(planId);
//        }
//        if(coursePlanClasses.getCategory() == CourseConstant.Category.EARLY.codeOf()&&courseType != CourseConstant.CourseType.AREA_BUILD.codeOf()&&courseType != CourseConstant.CourseType.KX_THEME.codeOf()){
//            vo = courseEarlyService.getEarlyCourseDetail(planId);
//        }
        if(courseType == CourseConstant.CourseType.AREA_BUILD.codeOf()){
            vo = courseAreaBuildService.getAreaBuildCourseDetail(planId);
        }
        if(courseType == CourseConstant.CourseType.KX_THEME.codeOf()){
            vo = courseKXThemeService.getKXThemeCourseDetail(planId);
        }
        return StatusResponse.success(vo);
    }

    @RequestMapping("/getMyWeekCourse")
    public StatusResponse getMyWeekCourse(@RequestHeader(TICKET) String ticket) {
        String weekStartDay = DateUtil.getWeekStartDay();
        String weekEndDay = DateUtil.getWeekEndDay();
        StaffDTO baseInfo = cache.getStaff(ticket);
        ClassesCoursePlanQuery query = new ClassesCoursePlanQuery();
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());
        query.setBeginDate(weekStartDay);
        query.setEndDate(weekEndDay);

        List<CoursePlanClassesDO> courseList =  coursePlanClassesService.getMyWeekCourseList(query);
        List<CoursePlanClassesDO> noPrepareList =courseList.stream().filter(o->o.getPrepareStatus()==0).collect(Collectors.toList());//未备课
        List<CoursePlanClassesDO> hasPrepareList =courseList.stream().filter(o->o.getPrepareStatus()==1).collect(Collectors.toList());//已备课
        List<CoursePlanClassesDO> waitConfirmList =courseList.stream().filter(o->o.getPrepareStatus()==2).collect(Collectors.toList());//待审核
        List<CoursePlanClassesDO> completeList =courseList.stream().filter(o->o.getCourseStatus()==1).collect(Collectors.toList());//已上课

        Map map = new HashMap<>();
        map.put("totalCount",courseList.size());
        map.put("noPrePareCount",noPrepareList.size());
        map.put("hasPrePareCount",hasPrepareList.size());
        map.put("waitConfirmCount",waitConfirmList.size());
        map.put("completeCount",completeList.size());

        return StatusResponse.success(map);

    }
    
    @RequestMapping("/get_KX_plan_list")
    public StatusResponse getKXPlanList(@RequestHeader(TICKET) String ticket,String classId,String queryType, ClassesCoursePlanQuery query){
        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = null ;
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());
        query.setClassId(classId);
        query.setType(queryType);
//        Boolean isTestUser = ssoCallService.getIsTestUser(baseInfo.getId());

        Classes classes = classesService.selectClassById(classId);
        //查询托班今日课程列表
        if(classes.getCategory() == CourseConstant.Category.NURSERY.codeOf() && "today".equals(queryType)){
            coursePlanNurseryList = courseNurseryService.getPadKXTodayCoursePlanList(query, false);
        }
        //查询托班本周课程列表
        if(classes.getCategory() == CourseConstant.Category.NURSERY.codeOf() && "week".equals(queryType)){
            coursePlanNurseryList = courseNurseryService.getPadKXWeekCoursePlanList(query, false);
        }
        //查询托班下周课程列表
        if(classes.getCategory() == CourseConstant.Category.NURSERY.codeOf() && "nextweek".equals(queryType)){
            coursePlanNurseryList = courseNurseryService.getPadKXCourseByNextWeekPlanList(query, false);
        }

        //查询早教班今日课程列表
        if(classes.getCategory() == CourseConstant.Category.EARLY.codeOf() && "today".equals(queryType)){
            coursePlanNurseryList =  courseEarlyService.getPadTodayCoursePlan(query, false);
        }
        //查询早教班本周课程列表
        if(classes.getCategory() == CourseConstant.Category.EARLY.codeOf() && "week".equals(queryType)){
            coursePlanNurseryList =  courseEarlyService.getPadWeekCoursePlan(query, false);
        }
        //查询早教班下周课程列表
        if(classes.getCategory() == CourseConstant.Category.EARLY.codeOf() && "nextweek".equals(queryType)){
            coursePlanNurseryList =  courseEarlyService.getPadNextWeekCoursePlan(query, false);
        }

        return StatusResponse.success(coursePlanNurseryList);

    }
    @ApiOperation(value = "(新)根据课程ID获取园区自建课程明细",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "courseId", value = "课程ID", required = true, dataType = "string")
    })
    @RequestMapping(value = "get_area_bulid_course_ById")
    public StatusResponse getAreaBulidCourseById(@RequestHeader String ticket, String courseId) {
        String areaId = cache.getStaff(ticket).getAreaId();
        CourseAreaBuildVo courseAreaBuildVo = courseService.getCourseAreaBuildByCourseId(areaId, courseId);
        return StatusResponse.success(courseAreaBuildVo);
    }
}


